package com.okq.service.Impl;

import com.okq.dao.CustomerDao;
import com.okq.dao.OrderDao;
import com.okq.entity.Customer;
import com.okq.entity.Order;
import com.okq.service.IOrderService;
import com.okq.util.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 描述 : 订单接口实现类
 * 作者 : zdl
 * 日期 : 2017/6/29 15:33
 */
@Transactional
@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private CustomerDao customerDao;

    @Override
    public void saveOrder(Order order) {
        orderDao.saveOrder(order);
    }

    /*
     * 删除订单记录
     */
    public void deleteOrder(Order order) {
        orderDao.deleteOrder(order);
    }

    /*
     * 更新订单记录
     */
    public void updateOrder(Order order) {
        orderDao.updateOrder(order);
    }

    @Override
    public void saveOrUpdateOrder(Order order) {
        orderDao.saveOrUpdateOrder(order);
    }

    @Override
    public Order getOrderById(String id) {
        return orderDao.getOrderById(id);
    }

    @Override
    public List<Order> listAllOrder() {
        return orderDao.listAllOrder();
    }

    @Override
    @SuppressWarnings("all")
    public Page<Order> pageByOrder(Page<Order> page, Order order) {
        if (order != null) {
            String customerKeyWord = order.getCustomerKeyWord();
            if (StringUtils.isNotBlank(customerKeyWord)) {
                List<Customer> cList = customerDao.listCustomerByKeyword(customerKeyWord);
                if (cList != null && !cList.isEmpty() && cList.size() > 0) {
                    String customerIds = "";
                    for (Customer c : cList) {
                        customerIds = c.getId() + ",";
                    }
                    if (StringUtils.isNotBlank(customerIds)) {
                        customerIds = customerIds.substring(0, (customerIds.length() - 1));
                        order.setCustomerIds(customerIds);
                    }
                }
            }
        }
        return orderDao.pageByOrder(page, order);
    }
	/**
	 * 描述 : 订单接口实现类
	 * 备注:	根据客户关键字查询
	 */
    @Override
    public Order getCustomerKeyWord(String customerKeyWord){
		return orderDao.getCustomerKeyWord(customerKeyWord); 	
    }
}
